import React, { useState, useEffect, useRef } from 'react';
import {
  Popover,
  Form,
  Row,
  Col,
  Select,
  Skeleton,
  Icon,
  Input,
  Button,
  Popconfirm,
  DatePicker,
  message,
  Drawer,
  Modal,
} from 'antd';
import { connect } from 'dva';
import request from '@/utils/request';
const judgeObj = obj => {
  if (Object.keys(obj).length == 0) return true;
  else return false;
};

function Edit(props) {
  const { getFieldDecorator } = props.form;
  const [loading, setLoading] = useState(false);

  const handleSubmit = () => {
    props.form.validateFields((err, fieldsValue) => {
      if (err) return;
      // console.log(fieldsValue,props.passData)
      setLoading(true);
      const paramsObj = {
        ...fieldsValue,
        createDate: props.passData.createDate,
        dataDictId: props.passData.dataDictId,
        // paramId:props.passData.paramId,
        statusDate: props.passData.statusDate,
        updateDate: props.passData.updateDate,
        updateStaff: props.passData.updateStaff,
      };
      request('pmc/pms/BmDataDictController/update', {
        data: paramsObj,
        method: 'POST',
      }).then(res => {
        if (res) {
          props.close(), props.refresh(), setLoading(false), message.success('保存成功！');
        } else {
          message.error('保存失败！'), setLoading(false);
        }
      });
    });
  };

  const handleCreate = () => {
    props.form.validateFields((err, fieldsValue) => {
      if (err) return;
      console.log(fieldsValue);
      setLoading(true);
      const paramsObj = {
        ...fieldsValue,
      };
      request('pmc/pms/BmDataDictController/add', {
        data: paramsObj,
      }).then(res => {
        if (res) {
          props.close(), props.refresh(), setLoading(false);
          message.success('创建成功！');
        } else {
          message.error('创建失败！');
          setLoading(false);
        }
      });
    });
  };

  return (
    <>
      <Form layout="vertical" onSubmit={handleSubmit}>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item label="组编码">
              {getFieldDecorator('groupCode', {
                rules: [
                  {
                    required: true,
                    message: '组编码不能为空',
                  },
                ],
              })(<Input placeholder="请输入" maxLength={80} />)}
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item label="组名称">
              {getFieldDecorator('groupName')(<Input placeholder="请输入" maxLength={200} />)}
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item label="参数编码">
              {getFieldDecorator('paramCode', {
                rules: [
                  {
                    required: true,
                    message: '参数编码不能为空',
                  },
                ],
              })(<Input placeholder="请输入" maxLength={80} />)}
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item label="参数名称">
              {getFieldDecorator('paramName')(<Input placeholder="请输入" maxLength={40} />)}
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item label="排序号">
              {getFieldDecorator('sortIdx')(<Input placeholder="请输入" maxLength={20} />)}
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item label="状态">
              {getFieldDecorator('statusCd', {
                initialValue: '1000',
              })(
                <Select placeholder="请选择">
                  <Option value="1000" key="1000">
                    有效
                  </Option>
                  <Option value="1100" key="1100">
                    无效
                  </Option>
                </Select>
              )}
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={16}>
          <Col span={24}>
            <Form.Item label="参数值">
              {getFieldDecorator('paramValue', {
                rules: [
                  {
                    required: true,
                    message: '参数值不能为空',
                  },
                ],
              })(<Input placeholder="请输入" maxLength={2000} />)}
            </Form.Item>
          </Col>
        </Row>
      </Form>

      <div
        style={{
          position: 'absolute',
          right: 0,
          bottom: 0,
          width: '100%',
          borderTop: '1px solid #e9e9e9',
          padding: '10px 16px',
          background: '#fff',
          textAlign: 'right',
        }}
      >
        {judgeObj(props.passData) ? (
          <Button type="primary" className="margin-right" onClick={handleCreate} loading={loading}>
            创建
          </Button>
        ) : (
          <Button type="primary" className="margin-right" onClick={handleSubmit} loading={loading}>
            保存
          </Button>
        )}
        <Button type="ghost" onClick={props.close}>
          取消
        </Button>
      </div>
    </>
  );
}

export default connect(({ paramsConfig }) => ({
  passData: paramsConfig.passData,
}))(
  Form.create({
    mapPropsToFields(props) {
      if (!judgeObj(props.passData)) {
        return {
          groupCode: Form.createFormField({ value: props.passData.groupCode }),
          groupName: Form.createFormField({ value: props.passData.groupName }),
          paramCode: Form.createFormField({ value: props.passData.paramCode }),
          paramName: Form.createFormField({ value: props.passData.paramName }),
          sortIdx: Form.createFormField({ value: props.passData.sortIdx }),
          statusCd: Form.createFormField({ value: props.passData.statusCd }),
          paramValue: Form.createFormField({ value: props.passData.paramValue }),
        };
      }
    },
  })(Edit)
);
